package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.ArticleDto;
import com.itheima.entity.*;
import com.itheima.service.*;
import com.itheima.untils.JwtUtils;
import com.itheima.util.MyPage;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/ydd_article")
@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CateService cateService;


    @GetMapping("/list")
    public R<MyPage> list(Long start_time, Long end_time, String title, Long cateId, Integer page, Integer limit){

        Page<YddArticle> pageInfo = new Page<>(page,limit);
        MyPage<ArticleDto>dtoPage  = new MyPage<>();
        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();

        yddArticleLambdaQueryWrapper.between(start_time!= null & end_time!= null,YddArticle::getCreateDate,start_time,end_time);
        yddArticleLambdaQueryWrapper.like(title!=null&title!="",YddArticle::getTitle,title);
        yddArticleLambdaQueryWrapper.eq(cateId!=null,YddArticle::getCateid,cateId);
        yddArticleLambdaQueryWrapper.orderByDesc(YddArticle::getCreateDate);
        articleService.page(pageInfo, yddArticleLambdaQueryWrapper);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<YddArticle> records = pageInfo.getRecords();
         List<ArticleDto> articleDtoList= records.stream().map((record)->{
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(record,articleDto);
            Integer cateid = record.getCateid();
            YddCate cate = cateService.getById(cateid);
            String cateName = cate.getCatename();

            if (cateName!=null){
                articleDto.setCatename(cateName);
            }
            return articleDto;
        }).collect(Collectors.toList());


        //总数
        dtoPage.setCount(pageInfo.getTotal());

        //总页数

        dtoPage.setTotalPages(pageInfo.getPages());

        //当前页码
        dtoPage.setCurrentPage(pageInfo.getCurrent());

        //每页数量
        dtoPage.setPageSize(pageInfo.getSize());

        dtoPage.setData(articleDtoList);
        return R.success(dtoPage);
    }


    @GetMapping("/del")
    public R<String> del(HttpServletRequest request, Integer id){
        String token = request.getHeader("x-token");
        if (StringUtils.isNotEmpty(token)) {
            Claims claims = JwtUtils.parseToken(token);
            String userId = (String) claims.get("id");
            articleService.removeById(id);
            return R.success("删除成功");
        }

        return R.error("删除文章失败");

    }

    @PostMapping("/add")
    public R<String> add(HttpServletRequest request,@RequestBody YddArticle article){
        String token = request.getHeader("x-token");
        if (StringUtils.isNotEmpty(token)) {
            Claims claims = JwtUtils.parseToken(token);
            String userId = (String) claims.get("id");
            article.setUserId(Integer.valueOf(userId));
            article.setCreateDate(System.currentTimeMillis());
            articleService.save(article);
            return R.success("添加成功");
        }

        return R.error("添加文章失败");
    }

    @GetMapping("/rowInfo")
    public R<YddArticle> getById(Integer id){
        YddArticle article = articleService.getById(id);
        return R.success(article);
    }
    @PostMapping("/update")
    public R<String> update(HttpServletRequest request,@RequestBody YddArticle article){
        String token = request.getHeader("x-token");
        if (StringUtils.isNotEmpty(token)) {
            Claims claims = JwtUtils.parseToken(token);
            String userId = (String) claims.get("id");
            article.setUserId(Integer.valueOf(userId));
            article.setCreateDate(System.currentTimeMillis());
            articleService.updateById(article);
            return R.success("修改成功");
        }

        return R.error("添加文章失败");
    }

}
